<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展 
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

namespace app\admin\model\wddj;

use app\admin\model\wddj\user\UserRealNameAuthentication;
use app\admin\model\wddj\user\Wechat;
use think\Model;


class Alarm extends Model
{

    

    

    // 表名
    protected $name = 'wddj_alarm';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'source_text',
        'status_text',
        'processing_time_text',
        'user_name',
    ];
    

    
    public function getSourceList()
    {
        return ['1' => __('Source 1'), '2' => __('Source 2')];
    }

    public function getStatusList()
    {
        return ['2' => __('Status 2'), '1' => __('Status 1')];
    }


    public function getSourceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source']) ? $data['source'] : '');
        $list = $this->getSourceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getProcessingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['processing_time']) ? $data['processing_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setProcessingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function wechat()
    {
        return $this->belongsTo('app\admin\model\wddj\user\Wechat', 'wechat_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getUserNameAttr($value,$data)
    {
        $result = '';
        if ($data['source'] == 1) {
            $userRealNameAuthenticationObj = (new UserRealNameAuthentication())
                ->where('wechat_id',$data['wechat_id'])
                ->find();
            if ($userRealNameAuthenticationObj) {
                $result = $userRealNameAuthenticationObj['name'];
            } else {
                $result = (new Wechat())->where('id',$data['wechat_id'])->value('nickname');
            }
        } else {
            $result = (new CertifiedDrivingAgent())
                ->where('wechat_id',$data['wechat_id'])
                ->value('name');
        }

        return $result;
    }
}
